using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Practices.CompositeUI;
using Jbe.TestSuite.Infrastructure.Interface.Services;

namespace Jbe.TestSuite.LogViewer.Demo
{
    public partial class LogDemo : UserControl, ILogDemo
    {
        private IEditManager editManager;
        private LogEntry logEntry;

        [ServiceDependency]
        public IEditManager EditManager
        {
            set 
            { 
                editManager = value;
                editManager.Register(eventIDText);
                editManager.Register(messageText);
                editManager.Register(priorityText);
            }
        }

        public LogEntry LogEntry
        {
            get { return logEntry; }
            set
            {
                logEntry = value;
                logEntryBindingSource.DataSource = logEntry;
            }
        }

        public LogDemo()
        {
            InitializeComponent();
            LogEntry = new LogEntry();
            severityCombo.DataSource = Enum.GetValues(typeof(TraceEventType));
        }

        protected override void OnLoad(EventArgs e)
        {
            presenter.OnViewReady();
            base.OnLoad(e);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                editManager.Deregister(eventIDText);
                editManager.Deregister(messageText);
                editManager.Deregister(priorityText);

                if (presenter != null)
                    presenter.Dispose();

                if (components != null)
                    components.Dispose();
            }

            base.Dispose(disposing);
        }

        private void SeverityComboSelectedValueChanged(object sender, EventArgs e)
        {
            LogEntry.Severity = (TraceEventType)severityCombo.SelectedValue;
        }

        private void CategoriesCheckedChanged(object sender, EventArgs e)
        {
            string category = ((CheckBox)sender).Text;
            bool check = ((CheckBox)sender).Checked;

            if (check && !LogEntry.Categories.Contains(category))
            {
                LogEntry.Categories.Add(category);
            }
            else if (!check && LogEntry.Categories.Contains(category))
            {
                LogEntry.Categories.Remove(category);
            }
        }

        private void LogEntryBindingSourceBindingComplete(object sender, BindingCompleteEventArgs e)
        {
            logButton.Enabled = ValidateChildren();
        }

        private void LogButtonClick(object sender, EventArgs e)
        {
            presenter.Log();
        }
    }
}

